package com.jec.protocol.pdu;

import com.jec.module.sysmonitor.entity.NetUnit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProtocolUtils {

	private static Map<Integer, NetUnit> netUnitCaches = new HashMap<>();

	public static void setNetUnitCaches(List<NetUnit> netUnits){
		if(netUnits==null)
			return;
		synchronized (netUnitCaches) {
			netUnitCaches.clear();
			for (NetUnit netUnit : netUnits)
				netUnitCaches.put(netUnit.getNetId(), netUnit);
		}
	}
	
	public static int getJump(PDU pdu) {
		return pdu.getInt8(0);
	}
	
	public static int getDestId(PDU pdu) {
		int netId = pdu.getInt8(1);
		NetUnit netUnit = netUnitCaches.get(netId);
		if(netUnit != null)
			netId = netUnit.getId();
		return netId;
	}
	
	public static int getSourId(PDU pdu) {
		int netId = pdu.getInt8(2);
		NetUnit netUnit = netUnitCaches.get(netId);
		if(netUnit != null)
			netId = netUnit.getId();
		return netId;
	}
	
	public static int getProtocolType(PDU pdu) {
		return pdu.getInt8(3);
	}
	
	public static int getBodySize(PDU pdu) {
		return pdu.getInt16(4);
	}
	
	public static int getCmdSN(PDU pdu) {
		return pdu.getInt8(6);
	}
	
	public static int getCmdType(PDU pdu) {
		return pdu.getInt8(7);
	}
	
	public static int getCmdCode(PDU pdu) {
		return pdu.getInt8(8);
	}	
	
	public static int getCmdConfig(PDU pdu) {
		return pdu.getInt8(9);
	}	
	
	public static int getCardType(PDU pdu) {
		return pdu.getInt8(10);
	}	
	
	public static int getCardSlot(PDU pdu) {
		return pdu.getInt8(11);
	}		

}
